/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package com.airlogger.log.view.format 
{
	import com.bourre.collection.HashMap;	
	import com.bourre.log.PixlibDebug;	
	import com.airlogger.log.model.LogData;	
	import com.airlogger.log.view.format.Formatter;
	
import flash.utils.getQualifiedClassName;	/**
	 * @author Cedric Nehemie
	 */
	public class SubArrayFormatter extends ArrayFormatter implements Formatter 
	{	
		public function SubArrayFormatter ( of : SubObjectFormatter = null )
		{
			super( false );
			map = new HashMap( );
			map.put( "String", new StringFormatter() );
			map.put( "XML", new SubXMLFormatter() );
			map.put( "Object", of ? of : new SubObjectFormatter( this ) );
			map.put( "Default", new DefaultFormatter() );
			map.put( "Array", this );
		}
		override public function format (data : LogData) : String
		{
			//PixlibDebug.DEBUG( "in object formatter " + data.type + ", " + data.message );
			var o : Array = data.message as Array;
			var s : String;
			if( o.length == 0 )
			{
				s = "Array [0]";
			}
			else
			{
				s = data.type + " [" + o.length + "]<ul><li><table class='array' cellpadding='0' cellspacing='1'>";
				var n : Number = 0;
				for( var i : String in o )
				{
					try
					{
						s += "<tr class='alternate" + (n%2==0?"1":"2") + "'><td class='index'>" +
							 i + "</td><td width='100%'><ul class='tree array'><li class='closed'>" +
							  _format ( new LogData( data.level,
		 							    o[ i ],
		 							    data.date,
								 		getQualifiedClassName( o[ i ] ) ) ) +
							 "</li></ul></td></tr>";
						n++;
					}
					catch ( e : Error )
					{
						PixlibDebug.DEBUG( "error " + e.getStackTrace() );					
					}		
				}
				
				s += "</table></li></ul>";
			}
			return s;
		}
	}
}
